﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Class_Entity;
using BussinessObject;
using System.Data;
using System.Data.SqlClient;


public partial class WebApplication_ADEditTablet : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            loadData();

        }
    }


    private void loadData()
    {
        int id = Convert.ToInt32(Session["ID"].ToString());

        Tablet ta = new Tablet();
        BOProducts pro = new BOProducts();
        // fill information into object ta
        ta.ID = id;
        // fill information of choosen object ta into text box
        DataTable dt = new DataTable();
        dt = pro.Find_Tablet(ta).Tables[0];
        txtModel.Text = dt.Rows[0]["Model"].ToString();
        txtDies.Text = dt.Rows[0]["Dies"].ToString();
        txtMaxP.Text = dt.Rows[0]["MaxPressure"].ToString();
        txtDia.Text = dt.Rows[0]["MaxDiameterOfTablet"].ToString();
        txtMaxDep.Text = dt.Rows[0]["MaxDepthOfFill"].ToString();
        txtCap.Text = dt.Rows[0]["ProductionCapacity"].ToString();
        txtSize.Text = dt.Rows[0]["Size"].ToString();
        txtWeight.Text = dt.Rows[0]["Weight"].ToString();


    }


    protected void btSubmit_Click(object sender, EventArgs e)
    {
        Tablet ta = new Tablet();
        BOProducts pro = new BOProducts();

        // fill information into object ta
        int id = Convert.ToInt32(Session["ID"].ToString());
        ta.ID = id;

        DataTable dt = new DataTable();
        dt = pro.Find_Tablet(ta).Tables[0];

        ta.Model = txtModel.Text;
        ta.Dies = txtDies.Text;
        ta.MaxDiameterOfTablet = Convert.ToInt32(txtDia.Text);
        ta.MaxPressure = Convert.ToInt32(txtMaxP.Text);
        ta.MaxDepthOfFill = Convert.ToInt32(txtMaxDep.Text);
        ta.ProductionCapacity = txtCap.Text;
        ta.Size = txtSize.Text;
        ta.Weight = txtWeight.Text;
        // if filename not change then use ole file name
        if (FileUploadImage.FileName.Equals(""))
        {
            ta.Image = dt.Rows[0]["Image"].ToString();
        }
        // filename changed then use and upload new image file
        else
        {
            ta.Image = "~\\WebApplication\\images\\" + FileUploadImage.FileName;
            FileUploadImage.SaveAs(Server.MapPath("~\\WebApplication\\images\\" + FileUploadImage.FileName));
        }

        // check if validation are true then add to database
        if (Session["a"] == null)
        {
            pro.Edit_Tablet(ta);
            HttpContext.Current.Response.Write("<script type='text/javascript'>alert('" + "Edit Successfully ..." + "');window.location.href='ADTablet.aspx';</script>");


        }
    }




    // check if model name exits or not
    protected void txtModel_TextChanged(object sender, EventArgs e)
    {
        Tablet ta = new Tablet();
        BOProducts bo = new BOProducts();
        DataSet ds = bo.Load_Tablet(ta);
        DataTable dt = ds.Tables[0];
        Label1.Text = "";
        Session["a"] = null;

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if (txtModel.Text.Trim() == dt.Rows[i]["Model"].ToString())
            {
                Session["a"] = 1;
                Label1.Text = "Model already exits";
            }

        }
    }
}